package jh.com.jhappbf.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;

/**
 * @author Administrator
 */
@SuppressLint("ServiceCast")
public class CargoNameFragment extends Fragment implements OnItemClickListener {
    private TextView mTxtTip;
    private ProgressBar mProgressBar;
    private ListView mListView;
    private List<JSONObject> mData;
    private ImageView mBackImageView;
    private TextView mTitleTextView;
    private TextView txtSetting1;
    private TextView txtSetting2;
    private MyAdapter mAdapter;
    private ImageView mLine;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.assay_list_view, null);
        mTxtTip = view.findViewById(R.id.ui_assay_data_tip);
        mProgressBar = view.findViewById(R.id.ui_assay_progress_bar);
        mListView = view.findViewById(R.id.ui_assay_listView);
        mListView.setOnItemClickListener(this);
        mData = new ArrayList<>();

        mBackImageView = getActivity().findViewById(R.id.ui_singleActivity_comeback);
        mLine = getActivity().findViewById(R.id.ui_singleActivity_line);
        mLine.setVisibility(View.GONE);
        mTitleTextView = getActivity().findViewById(R.id.ui_singleActivity_title);
        mTitleTextView.setText("货名选择");
        txtSetting1 = getActivity().findViewById(R.id.ui_singleActivity_txt1);
        txtSetting1.setVisibility(View.GONE);
        txtSetting2 = getActivity().findViewById(R.id.ui_singleActivity_txt2);
        txtSetting2.setVisibility(View.GONE);
        mAdapter = new MyAdapter();
        mListView.setAdapter(mAdapter);
        getData();
        return view;
    }

    /**
     * summary: 获取数据
     */
    private void getData() {
        String methodName = "getCaroName";
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();

        mListView.setVisibility(View.GONE);
        mProgressBar.setVisibility(View.VISIBLE);
        mTxtTip.setVisibility(View.GONE);

        Listener<String> listener = response -> {
            mData = JsonHelper.getJsonObjects(response);
            if (mData != null && mData.size() > 0) {
                mListView.setVisibility(View.VISIBLE);
                mProgressBar.setVisibility(View.GONE);
                mTxtTip.setVisibility(View.GONE);
                mAdapter.notifyDataSetChanged();
            } else {
                mListView.setVisibility(View.GONE);
                mProgressBar.setVisibility(View.GONE);
                mTxtTip.setVisibility(View.VISIBLE);
                mTxtTip.setText("没有获取到数据！");
            }
        };

        ErrorListener errorListener = error -> {
            mListView.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.GONE);
            mTxtTip.setVisibility(View.VISIBLE);
            mTxtTip.setText("异常发生:" + error.getMessage());
        };

        VolleyCallWebService request = new VolleyCallWebService(methodName, param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(getActivity()).addRequest(request);
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(getActivity(),R.layout.assay_list_item, null);
                holder.txt = convertView.findViewById(R.id.assay_list_item_content);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            try {
                JSONObject object = mData.get(position);
                holder.txt.setText(object.getString("货名"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return convertView;
        }

        class ViewHolder {
            TextView txt;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent();
        JSONObject object = mData.get(position);
        String value = "";
        try {
            value = object.getString("货名");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        i.putExtra("result", value);
        getActivity().setResult(2, i);
        getActivity().finish();
    }

}
